const http = require('http')
const querystring = require("querystring")
const url = require('url')
const postData = querystring.stringify({
    name: "李四",
    age: 23,
    sex: 0
})
const app = http.createServer((req, res) => {
    let data = ''
    let urlstr = req.url;
    let urlObj = url.parse(urlstr, true)
    res.writeHead(200, {
        'content-type': "application/json;chartset=utf-8",
        "Access-Control-Allow-Origin": "*"    //这个就是解决跨域的
    })
    req.on('data', (chunk) => {
        console.log(chunk,000);
        data += chunk
    })
    req.on('end', () => {
        reponseResult(querystring.parse(postData))
    })
    function reponseResult(data) {
        switch (urlObj.pathname) {
            case '/api/data':
             
                res.end(JSON.stringify({message:data}))
                break;

            default:
                res.end('not fond')
                break;
        }
    }

})

app.listen('3000', () => {
    console.log('q');
})
